<?php

namespace app\index\controller;

use think\Db;

class User extends CommonController
{
    const ROLE_COMPANY = 1;         // 保险公司人员
    const ROLE_CODER = 2;           // 代录人
    const ROLE_PROTECT = 3;         // 被保险人
    const ROLE_SALE_MAN = 4;        // 业务员

    public $request;

    public function __construct(\think\Request $request)
    {
        parent::__construct($request);
        $this->request = $request->param();
    }


    /**
     * 所有角色查询个人信息（通用）
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getUserInfo()
    {
        $user_id = $this->uid;
        $res = Db::table('user_info')->where('ui_id',$user_id)->find();
        switch ($res['role_id']){
            case self::ROLE_COMPANY:
                $field = [
                    'user_info.head_img',
                    'user_info.username',
                    'user_info.mobile',
                    'user_info.company_name',
                    'user_info.card_num',
                ];
                $data = Db::table('user_info')->where('ui_id',$user_id)->field($field)->find();
                break;
            case self::ROLE_CODER:
                $field = [
                    'user_info.head_img',
                    'user_info.username',
                    'user_info.mobile',
                    'user_info.company_name',
                    'user_info.card_num',
                ];
                $data = Db::table('user_info')->where('ui_id',$user_id)->field($field)->find();
                $a = Db::table('user_code_r')->where('recoder_id',$user_id)->find();
                $data['rem_link_name'] = $a['rem_link_name'];
                $data['rem_link_phone'] = $a['rem_link_phone'];
                break;
            case self::ROLE_PROTECT:
                $field = [
                    'user_info.head_img',
                    'user_info.username',
                    'user_info.mobile',
                    'user_info.company_name',
                ];
                $data = Db::table('user_info')->where('ui_id',$user_id)->field($field)->find();
                break;
            case self::ROLE_SALE_MAN:
                $field = [
                    'user_info.head_img',
                    'user_info.username',
                    'user_info.mobile',
                    'user_info.company_name',
                    'user_info.card_num',
                    'user_info.sale_man_company',
                    'user_info.sale_man_linkman',
                ];
                $data = Db::table('user_info')->where('ui_id',$user_id)->field($field)->find();
                break;
        }
        return $this->response->json(['data'=>$data,'code'=>self::SUCCESS_CODE,'msg'=>'']);
    }

    /**
     * 用户资料修改
     * @return \think\Response
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function edit_info()
    {
        if(isset($this->request['username']) && !empty($this->request['username'])){
            $data['username'] = $this->request['username'];
        }
        if(isset($this->request['head_img']) && !empty($this->request['head_img'])){
            $data['head_img'] = $this->request['head_img'];
        }
        if(isset($this->request['card_num']) && !empty($this->request['card_num'])){
            $data['card_num'] = $this->request['card_num'];
        }
        if(isset($this->request['company_name']) && !empty($this->request['company_name'])){
            $data['company_name'] = $this->request['company_name'];
        }
        $data['updated_at'] = date('Y-m-d H:i:s',time());
        $res = Db::table('user_info')->where('ui_id',$this->uid)->update($data);
        if($res){
            return $this->response->api(['data'=>'','code'=>self::SUCCESS_CODE,'msg'=>'']);
        }else{
            return $this->response->api(['data'=>'','code'=>self::ERROR_CODE,'msg'=>'']);
        }
    }


    public function user_question()
    {
        $res = Db::table('question')->where('status',1)->field('id,question_name,content')->select();
        return $this->response->api(['data'=>$res,'code'=>self::SUCCESS_CODE,'msg'=>'']);
    }


}
